package com.csii.pos.uofplan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.util.excel.ExcelUtils;
import com.csii.pos.query.ExcelTemplate;
import com.csii.pos.query.service.IExcelTemplateService;
import com.csii.pos.query.vo.CashDailyVO;
import com.csii.pos.uofplan.service.UofPlanService;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuzhonghua
 * @version 1.0
 * @description: 制定资金计划页面
 * @date 2021.6.22
 */
@RestController
@Slf4j
@RequestMapping("/uofPlan")
public class UofPlanController {
    @Resource
    private UofPlanService uofPlanService;
    @Autowired
    private IExcelTemplateService excelTemplateService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * @description: 制定资金计划新增
     * @param: uofPlanPo
     * @return:
     * @author liuzhonghua
     * @date: 2021.6.22
     */
    @RequestMapping("addDeptPlan")
    public APIResult<?> addUofPlan(@Validated @RequestBody UofPlanPo uofPlanPo) {
        UofPlanPo uofPlan = uofPlanService.getUofPlan(uofPlanPo);
        if (uofPlan!=null){
            return APIResult.failResult(null,"已存在相同时间数据");
        }
        //-----------------------  changed by zhanghao  ----------------------------//
        Map map = uofPlanService.addUofPlan(uofPlanPo);
        return APIResult.successResult(map);
        //-----------------------   change end  ------------------------------------//

        /*int add = uofPlanService.addUofPlan(uofPlanPo);
        String MESSAGE = "头寸操作失败！";
        if (add > 0) {
            return APIResult.successResult(null);
        } else {
            MESSAGE = "制定头寸计划失败！";

            if (add == -1) {
                MESSAGE = "超出制定计划时间！";
            }

            return APIResult.failResult(null, MESSAGE);
        }*/

    }

    /**
     * @description: 制定资金计划删除
     * @param: list
     * @return: null
     * @author liuzhonghua
     * @date: 2021.6.22
     */
    @RequestMapping("deleteDeptPlan")
    public APIResult<?> deleteUofPlan(@RequestBody UofPlanPo uofPlanPo) {
        uofPlanService.deleteUofPlan(uofPlanPo);
        return APIResult.successResult("删除成功");
    }

    /**
     * @description: 制定资金计划修改
     * @param: uofPlanPo
     * @return: null
     * @author liuzhonghua
     * @date: 2021.6.22
     */
    @RequestMapping("editDeptPlan")
    public APIResult<?> updateUofPlan(@RequestBody UofPlanPo uofPlanPo) {
        uofPlanService.updateUofPlan(uofPlanPo);
        return APIResult.successResult("更新成功");
    }

    /**
     * @description: 制定资金计划查询
     * @param: uofPlanPo
     * @return: 当日计划的集合
     * @author liuzhonghua
     * @date: 2021.6.23
     */
    @RequestMapping("queryDeptPlan")
    public APIResult<?> queryUofPlan(@RequestBody UofPlanPo uofPlanPo) {
        List<UofPlanPo> operationLogIPage = uofPlanService.queryUofPlan(uofPlanPo);

        return APIResult.successResult(operationLogIPage);
    }

    /**
     * @description: 制定资金计划查询所有金额
     * @param: uofPlanPo
     * @return: 总金额
     * @author liuzhonghua
     * @date: 2021.6.23
     */
    @RequestMapping("getAllAmtByCondition")
    public APIResult<?> getAllAmtByCondition(@RequestBody UofPlanPo uofPlanPo) {

        BigDecimal uofItemAmts = uofPlanService.getAllAmtByCondition(uofPlanPo);
        return APIResult.successResult(uofItemAmts);

    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response,@RequestParam Long uofItemSn,
                          @RequestParam String uofPlanType) {
        QueryWrapper<ExcelTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TABLE_NAME", "UOF_PLAN");
        ExcelTemplate template = excelTemplateService.getOne(queryWrapper);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        System.out.println(uofPlanType);
        //当前日期
        String format = df.format(new Date());
        HashMap<String, String> map = new HashMap<>();
        map.put("$UOF_ITEM_SN", String.valueOf(uofItemSn));
        if ("tmp".equals(uofPlanType)) {
            map.put("$type", "临时");
        }else if ("week".equals(uofPlanType)) {
            map.put("$type", "周计划");
        }else {
            map.put("$type", "月计划");
        }
        ExcelUtils.exportTemplate(template.getTemplateName(), template.getTemplateDir(), format,map, jdbcTemplate, response);
    }

    /**
     * @description: 查询部门名称
     * @param: uofPlanPo必须包含部门编号
     * @return: 总部门
     * @author liuzhonghua
     * @date: 2021.6.23
     */
    @RequestMapping("getDepartName")
    public APIResult<?> getDepartName(@RequestBody UofPlanPo uofPlanPo) {
        return APIResult.successResult(uofPlanService.getDepartName(uofPlanPo));
    }
}
